#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os.path
import re
import tornado.database
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import base64
import hashlib
import basehandler

# Auth Handler
class AuthHandler(basehandler.BaseHandler):
    def get(self,*args):
        self.clear_all_cookies()
        self.render("login.html")
    
    def post(self,*args):
        userCenter = ['user','business']
        password = hashlib.md5(self.get_argument("password",'none')).hexdigest()
        email = self.get_argument("email",'none')
        user = self.db.get("select * from users where `email`=%s and `password`=%s",email,password)
        if user:
            self.set_secure_cookie("uid",str(user.id))
            self.set_secure_cookie("gid",str(user.group))
            self.set_secure_cookie("email",user.email)
            if user.group !=9:
                self.set_secure_cookie("center",userCenter[int(user.group)])
                self.redirect("/"+userCenter[int(user.group)]+"/")
            else:
                self.set_secure_cookie("center","master")
                self.redirect("/admin/")
        else:
            self.redirect("/login")
